using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Xml.Linq;


namespace tranquility
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public abstract class LevelComponent : Microsoft.Xna.Framework.GameComponent
    {
        public string WallsFile { get; set; }
        public List<Wall> Walls { get; set; }

        public LevelComponent(Game1 game, string wallsFile)
            : base((Game)game)
        {
            WallsFile = wallsFile;
            System.IO.Stream stream = TitleContainer.OpenStream(wallsFile);
            XDocument doc = XDocument.Load(stream);
            Walls = XmlReader(doc);

            foreach (Wall wall in Walls)
            {
                Game.Components.Add(wall);
            }
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override abstract void Initialize();

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override abstract void Update(GameTime gameTime);

        List<Wall> XmlReader(XDocument doc)
        {
            List<Wall> walls = new List<Wall>();
            walls = (from wall in doc.Descendants("wall")
                     select new Wall(Game)
                     {
                         Position = new Vector3(Convert.ToInt32(wall.Element("positionX").Value),
                                             Convert.ToInt32(wall.Element("positionY").Value),
                                             Convert.ToInt32(wall.Element("positionZ").Value)),

                         Size = new Vector3(Convert.ToInt32(wall.Element("sizeX").Value),
                                         Convert.ToInt32(wall.Element("sizeY").Value),
                                         Convert.ToInt32(wall.Element("sizeZ").Value))

                     }).ToList();
            return walls;
        }
    }
}
